<?php
/**
 * 会员中心，查询订单，留言，积分,充值等
 *
 * @author Mike
 */
class MemberAction extends CommonAction {
    /** 检查用户是否登录
	 */
	protected function _initialize() {
		if (!isset($_SESSION[C('USER_AUTH_KEY')])) {
			$this->assign('jumpUrl', __APP__.'/Index/login/');
			$this->error('请先登录！');
		}
		parent::_initialize();
	}

    public function index(){
        import("@.ORG.Util.Page");
        $entry = $_GET['entry'];
        $count_checkin = M('Checkin')->where('member_id ='.$_SESSION[C('USER_AUTH_KEY')] )->order('create_time desc')->count('id');
        $count_notice =  M('Comment')->where('member_id ='.$_SESSION[C('USER_AUTH_KEY')].' and user_id !=0')->order('create_time desc')->count('id');
        $count_message = M('Comment')->where('member_id ='.$_SESSION[C('USER_AUTH_KEY')].' and user_id =0' )->order('create_time desc')->count('id');
        $count_order = M('Order')->where('member_id ='.$_SESSION[C('USER_AUT                                                                                                                                     H_KEY')] )->order('create_time desc')->count('id');
        $count_costs = D('CostdetailView')->where('member_id ='.$_SESSION[C('USER_AUTH_KEY')] .' and Costdetail.status =2 and cost_type = 0')->count('Costdetail.id');  //未付款的房租
        
        $count_read_order = M()->query(
            "select count(l.id) as cl from   ok_order as od
left join ok_member as m on od.member_id = m.id
left join ok_msg_log as l on od.id = l.obj_id
where l.obj = 'order' and l.is_read = 1 and m.id = ".$_SESSION[C('USER_AUTH_KEY')]); 

        $count_od = ($count_order - $count_read_order['0']['cl']); //未阅读的订单

        $count_read_msg_str = "select count(c.id) as cid from ok_comment as c 
left join ok_msg_log as l  on c.id = l.obj_id 
where l.obj = 'comment' and l.is_read = 1 ";
        //统计已经阅读的个人留言
        $count_read_message = M()->query( $count_read_msg_str.' and member_id ='.$_SESSION[C('USER_AUTH_KEY')].' and user_id =0');
        $count_read_notice = M()->query( $count_read_msg_str.' and member_id ='.$_SESSION[C('USER_AUTH_KEY')].' and user_id !=0');
        $count_mg = $count_message - $count_read_message['0']['cid'];
        $count_sg = $count_notice - $count_read_notice['0']['cid'];
        $this->assign(array(
            'count_checkin' =>$count_checkin,
            'count_notice' => $count_notice,
            'count_message' => $count_message,
            'count_order' => $count_order,

            'memberMenu' => 'memberMenu',
            
            'count_od' => $count_od,    //最新订单
            'count_mg' => $count_mg,    //new msg count
            'count_sg' => $count_sg,    //new system meessage
            'count_costs' => $count_costs,    //未付房租统计
        ));

        $Page = new Page($countHotel,10);//每页显示10个
        $show = $Page ->show();
        $list = D('HotelView')->where($map)->limit($Page->firstRow.",".$Page->listRows)->order('sort desc,create_time asc')->findAll();

        switch ($entry){
            case 'account':
                $this->assign('payper_type',C('Paypers'));
                $this->assign('userinfo',M('Member')->where('id ='.$_SESSION[C('USER_AUTH_KEY')] )->find());
                $this->assign('content','account');
                $this->assign('account','hover');
                $this->display();
            break;
        case 'checkin': //入住消费明细
                $Page = new Page($count_checkin,15);//每页显示10个
                $this->assign('checkins',M('Checkin')->where('member_id ='.$_SESSION[C('USER_AUTH_KEY')] )->limit($Page->firstRow.",".$Page->listRows)->order('create_time desc')->findAll());
                $this->assign('page',$Page->show());
                $this->assign('content','checkin');
                $this->assign('checkin','hover');
                $this->display();
            break;
        case 'notice': //公告
                $Page = new Page($count_notice,15);//每页显示10个
                $this->assign('messages',M('Comment')->where('member_id ='.$_SESSION[C('USER_AUTH_KEY')].' and user_id !=0')->limit($Page->firstRow.",".$Page->listRows)->order('create_time desc')->findAll());
                $this->assign('page', $Page ->show() );
                $this->assign('content','notice');
                $this->assign('notice','hover');
                $this->display();
            break;
        case 'message':
                $Page = new Page($count_message,15);//每页显示10个
                $this->assign('messages',M('Comment')->where('member_id ='.$_SESSION[C('USER_AUTH_KEY')].' and user_id =0' )->limit($Page->firstRow.",".$Page->listRows)->order('create_time desc')->findAll());
                $this->assign('page',$Page->show());
                $this->assign('content','message');
                $this->assign('message','hover');
                $this->display();
            break;
        case 'score'://积分
                $this->assign('content','score');
                $this->display();
            break;
        case 'orderdetail'://订单详情
             $id = intval( $_GET['id'] );
             //更新日志
             updateLogStatus('order',$id,1);

             $this->assign('order',M('Order')->where('id ='.$id.' and member_id ='.$_SESSION[C('USER_AUTH_KEY')])->find());
             $this->assign('content','orderdetail');
             $this->assign('order_active','hover');
             $this->display();
            break;
        case 'checkindetail'://入住单详情
            $id = intval( $_GET['id'] );
            $this->assign('cost_status',C('borrowstatus'));//借出记录
            $this->assign('cost_status_xf',C('coststatus'));//消费记录
            $this->assign('cost_types',C('Cost_type'));//费用类型
            $this->assign('costs',M('Costdetail')->where('checkin_id ='.$id)->findAll() );
            $this->assign('borrows',M('Borrow')->where('checkin_id ='.$id)->findAll() );
            $this->assign('vo',M('Checkin')->where('id ='.$id.' and member_id ='.$_SESSION[C('USER_AUTH_KEY')])->find());
            $this->assign('content','checkindetail');
            $this->assign('checkin','hover');
            $this->display();
            break;
         case 'msgdetail'://留言详情
             $id = intval( $_GET['id'] );
             //更新日志
             updateLogStatus('comment',$id,1);

             $this->assign('msg',M('Comment')->where('id ='.$id.' and member_id ='.$_SESSION[C('USER_AUTH_KEY')])->find());
             $this->assign('content','msgdetail');
             $this->assign('message','hover');
             $this->display();
            break;
        case 'noticedetail'://公寓消息详情
             $id = intval( $_GET['id'] );
             //更新日志
             updateLogStatus('comment',$id,1);

             $this->assign('msg',M('Comment')->where('id ='.$id.' and member_id ='.$_SESSION[C('USER_AUTH_KEY')])->find());
             $this->assign('content','noticedetail');
             $this->assign('notice','hover');
             $this->display();
            break;
        case 'order':
        default:
            $Page = new Page($count_order,15);//每页显示10个
            $this->assign('orders',M('Order')->where('member_id ='.$_SESSION[C('USER_AUTH_KEY')] )->limit($Page->firstRow.",".$Page->listRows)->order('create_time desc')->findAll());
            $this->assign('page',$Page->show());
            $this->assign('content','order');
            $this->assign('order_active','hover');
            $this->display();

        }
    }

    public function resetPw(){
        if(empty($_POST['password'])){
            $this->error("请输入密码！");
        }
        elseif($_POST['password'] == $_POST['repassword'] ){
            $res = M('Member')->where('id ='.$_SESSION[C('USER_AUTH_KEY')])->setField('password',md5($_POST['password']));
            if(false == $res){
                 $this->error("密码重置出现问题了，请返回重新设置！");
            }
            else{
                $this->success("密码修改成功！");
            }
        }
        else{
            $this->error("请输入相同的密码！");
        }
    }

    public function memberEdit(){
         if(empty($_POST['name'])){
            $this->error("请输入真实姓名！");
        }
        $Member = D('Member');
        if(false == $stat = $Member->create()){
            $this->error($Member->getError());
        }
        if(false == $svStat = $Member->save()){
            $this->error('信息修改失败，请重新修改！');
        }
        else{
            $this->success('恭喜您，您的信息已修改成功！');
        }
    }

    public function cancelOrder(){
        if(empty($_REQUEST['oid'])){
            $this->error("请选择要取消的订单！");
        }
        $rs = M('Order')->where('member_id ='.$_SESSION[C('USER_AUTH_KEY')].' and id ='.$_REQUEST['oid'])->find();
        if(!is_array($rs)){
            $this->error("您无权取消该订单！");
        }else{
            M('Order')->where('id ='.$_REQUEST['oid'])->setField('status',6);   //客户取消订单
            $this->success('您成功取消该订单！');
        }
    }
    
}
?>
